package com.example.my_self_view

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.example.my_self_view.databinding.ActivityViewPagerBinding

class ViewPagerActivity: AppCompatActivity() {

    private lateinit var mBinding: ActivityViewPagerBinding
    private val items = arrayListOf("直播", "推荐", "视频", "图片", "段子", "精华")
    private val mIndicators by lazy { mutableListOf<ColorTrackTextView>() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityViewPagerBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        initIndicator()
        initViewPager()
    }

    private fun initIndicator() {
        for ((i, v) in items.withIndex()) {
            // 动态添加颜色跟踪的TextView
            val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
            params.weight = 1f
            val colorTrackTextView = ColorTrackTextView(this)
            // 设置颜色
            colorTrackTextView.textSize = 20f
            colorTrackTextView.text = v
            colorTrackTextView.setChangeColor(Color.RED)
            colorTrackTextView.layoutParams = params
            // 把新的加入LinearLayout容器
            mBinding.indicatorView.addView(colorTrackTextView)
            // 加入集合
            mIndicators.add(colorTrackTextView)
        }
    }

    private fun initViewPager() {
        mBinding.viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getCount(): Int {
                return items.size
            }

            override fun getItem(position: Int): Fragment {
                return ItemFragment.newInstance(items[position])
            }
        }

        mBinding.viewPager.addOnPageChangeListener(object : OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                // position 代表当前的位置
                // positionOffset 代表滚到 0-1 之间 （百分比）

                // 1.左边 位置  position
                val left = mIndicators[position]
                left.setDirection(ColorTrackTextView.Direction.RIGHT_TO_LEFT)
                left.setCurrentProgress(1f - positionOffset)

                // 2.右边
                if (position + 1 >= mIndicators.size) return
                val right = mIndicators[position + 1]
                right.setDirection(ColorTrackTextView.Direction.LEFT_TO_RIGHT)
                right.setCurrentProgress(positionOffset)
            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }
}